﻿<!DOCTYPE html>
<html>
<head>
    <title>Place Searches</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }

    </style>
    <script>
      var urlParams = new URLSearchParams(window.location.search);
      var place = urlParams.get('place');
      var map;
      var service;
      var infowindow;

    function initMap() {

        var msftVisitorCenter = new google.maps.LatLng(47.642232, -122.1389797);
        infowindow = new google.maps.InfoWindow();

        var mapOptions = {
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            center: msftVisitorCenter,
            disableDefaultUI: true
        };
        map = new google.maps.Map(
            document.getElementById('map'), mapOptions);

        var marker = new google.maps.Marker({
            position: msftVisitorCenter,
            map: map
        });

        var request = {
          query: place,
          fields: ['name', 'geometry'],
        };

        service = new google.maps.places.PlacesService(map);

        service.findPlaceFromQuery(request, function(results, status) {
          if (status === google.maps.places.PlacesServiceStatus.OK) {
            for (var i = 0; i < results.length; i++) {
              createMarker(results[i]);
            }

            map.setCenter(results[0].geometry.location);
          }
        });
      }

      function createMarker(place) {
        var marker = new google.maps.Marker({
          map: map,
          position: place.geometry.location
        });

        google.maps.event.addListener(marker, 'click', function() {
          infowindow.setContent(place.name);
          infowindow.open(map, this);
        });
      }

    </script>
</head>
<body>
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"
        async defer></script>
</body>
</html>